package com.gzc.just.play.last.war.robotclient.ai;

import com.gzc.just.play.last.war.robotclient.RobotAI;
import com.gzc.just.play.last.war.robotclient.RobotPlayer;

import java.util.Random;

/**
 * Move AI
 * Corresponds to C++ version CPlayerMoveAI
 */
public class MoveAI extends RobotAI {
    private final Random random = new Random();
    
    // Move related parameters
    private long lastMoveTime = 0;
    private static final long MOVE_INTERVAL = 3000; // 3 seconds move interval
    
    public MoveAI(RobotPlayer player) {
        super(player);
    }
    
    @Override
    public int begin() {
        logInfo("Move AI started");
        return 0;
    }
    
    @Override
    public int update(long currentTime) {
        if (getAIState() == AIState.RUNNING) {
            // Check if it's time to move
            if (currentTime - lastMoveTime > MOVE_INTERVAL) {
                // Perform random movement
                performRandomMove();
                lastMoveTime = currentTime;
                
                // Return AI continue running
                setAIState(AIState.RUNNING);
                return 1;
            }
        }
        
        return 0;
    }
    
    @Override
    public int end() {
        logInfo("Move AI ended");
        setAIState(AIState.CLEARED);
        return 0;
    }
    
    /**
     * Perform random movement
     */
    private void performRandomMove() {
        // Generate random position within a reasonable range
        double x = (random.nextDouble() * 200) - 100; // -100 to 100
        double y = (random.nextDouble() * 200) - 100; // -100 to 100
        double z = 0; // Flat ground
        
        // Update player position
        getPlayer().updateState(RobotPlayer.PlayerState.MOVING, x, y, z);
        
        logInfo("Robot {} moved to position ({}, {}, {})", 
                getPlayer().getRobotId(), x, y, z);
    }
}